Method and Apparatus for Generating Virtual Machine Snapshot

ABSTRACT

A method and an apparatus for generating a virtual machine snapshot, where the method includes suspending a virtual machine according to a received snapshot command, enabling write protection on a memory page of the virtual machine in a user mode, wherein the write protection causes the memory page to be blocked in a kernel mode and a write protection exception to be processed in the user mode, storing the memory page to a snapshot file before the memory page is contaminated, restoring the virtual machine after the storage is completed.

CROSS-REFERENCE TO RELATED APPLICATIONS

This application is a continuation of U.S. patent application Ser. No.15/991,858 filed on May 29, 2018, which is a continuation ofInternational Patent Application No. PCT/CN2016/079882, filed on Apr.21, 2016, which claims priority to Chinese Patent Application No.201510863046.0 filed on Nov. 30, 2015. All of the afore-mentioned patentapplications are hereby incorporated by reference in their entireties.

TECHNICAL FIELD

The present disclosure relates to the computer field, and in particular,to a method and an apparatus for generating a virtual machine snapshot.

BACKGROUND

A virtual machine snapshot means copying and storing a status, which isat a specific moment, of a virtual machine in order to restore thevirtual machine according to the virtual machine snapshot when a systemencounters a problem, or restore the virtual machine to a running stateat the moment according to the virtual machine snapshot after thevirtual machine starts. An online snapshot means storing a workingstatus of a current virtual machine to a specified file withoutinterrupting normal running of the current virtual machine.

The virtual machine snapshot includes a device status and memory data ofthe virtual machine. At present, a kernel-based virtual machine (KVM)does not have an online memory snapshot function. When a snapshot of theKVM is being obtained, the virtual machine needs to be suspended first,and then content in a virtual memory of the virtual machine and thedevice status are live migrated to a snapshot file. However, livemigration from the memory of the virtual machine to the snapshot filetakes a relatively long time, and therefore the virtual machine issuspended for a relatively long time, greatly affecting a service of thevirtual machine.

SUMMARY

Embodiments of the present disclosure provide a method and an apparatusfor generating a virtual machine snapshot in order to ensure consistencyof memory of a virtual machine in a snapshot file, and reduceinterruption of an internal service of the virtual machine as far aspossible.

According to a first aspect, an embodiment of the present disclosureprovides a method for generating a virtual machine snapshot. Accordingto the method, write protection is performed on all memory pages of avirtual machine in a user mode such that all write operations on amemory page of the virtual machine are blocked in a kernel mode, a writeprotection exception is processed in the user mode, the memory page isstored to a snapshot file before being contaminated, and after thestorage is completed, running of the virtual machine immediatelyresumes. Therefore, consistency of memory of the virtual machine in thesnapshot file is ensured, normal running of the virtual machine is notaffected as far as possible, and impact on a host machine is mitigated(which mainly means that excessive extra memory cannot be occupied).

In a first possible implementation of the first aspect, a virtualmachine monitor obtains a snapshot command, suspends the virtual machineat a first moment according to the snapshot command, and stores a devicestatus, which is at the first moment, of the virtual machine in asuspended state to the snapshot file of the virtual machine. The virtualmachine is restored from the suspended state to a running state afterthe device status is stored. From the first moment, the virtual machinemonitor further performs a storage operation on a memory page in memoryof the virtual machine and a contamination interception operation on thememory page in the memory of the virtual machine.

According to the first possible implementation of the first aspect, in asecond possible implementation, the contamination interception operationon the memory page in the memory of the virtual machine includesintercepting, from the first moment to a moment at which content of thememory page is stored to the snapshot file of the virtual machine, acontamination action that affects the content of the memory page, andafter storing the content of the memory page to a buffer, releasing theinterception on the contamination action.

According to the first or the second possible implementation of thefirst aspect, in a third possible implementation, the storage operationon the memory page in the memory of the virtual machine includes storingthe content, stored in the buffer, of the memory page to the snapshotfile of the virtual machine. According to this embodiment of the presentdisclosure, the buffer is used to store the memory page of the virtualmachine to mitigate impact on running of the virtual machine. A user mayset a size of the buffer according to an actual requirement, and alarger buffer indicates smaller impact on the running of the virtualmachine.

According to any one of the first aspect or the possible implementationsof the first aspect, in a fourth possible implementation, the storageoperation on the memory page in the memory of the virtual machine andthe contamination interception operation on the memory page in thememory are two actions that are performed in parallel after the firstmoment.

According to any possible implementation of the first aspect, in a fifthpossible implementation, before obtaining a snapshot file of one memorypage of the memory pages, a process of sequentially obtaining snapshotfiles of memory pages in the memory of the virtual machine includesdetermining whether content of the memory page has been stored to thebuffer, and directly storing the content of the memory page to thesnapshot file if the content of the memory page has not been stored tothe buffer, or storing the content, in the buffer, of the memory page tothe snapshot file of the virtual machine if the content of the memorypage has been stored to the buffer. Therefore, all the memory pages ofthe virtual machine are stored only once, thereby ensuring that a sizeof the snapshot file is less than or equal to a size of the memory ofthe virtual machine, and reducing space occupied by the snapshot file ofthe memory.

Further, in a sixth possible implementation, the buffer may be memoryother than the memory of the virtual machine, or may be multiple filesother than the memory of the virtual machine.

According to any possible implementation of the first aspect, in aseventh possible implementation, a method for determining whether thecontent of the memory page has been stored to the buffer includesflagging the memory page as “contamination action intercepted” when thecontamination action that affects the content of the memory page isintercepted, or flagging the memory page as “stored to the buffer” whenthe content of the memory page is stored to the buffer.

In the method for generating a virtual machine snapshot in thisembodiment of the present disclosure, the virtual machine is restored tothe running state after the snapshot file of the device status, which isat the first moment, of the virtual machine is obtained such that thevirtual machine is suspended for a relatively short time, and impact ona service of the virtual machine is relatively small. When a snapshotfile of the memory page of the virtual machine is being obtained, fromthe first moment to the moment at which the content of the memory pagein the memory of the virtual machine is stored to the snapshot file ofthe virtual machine, the contamination action that affects the contentof the memory page is intercepted, and the content of the memory page isstored to the buffer such that when the memory page of the virtualmachine is being stored, the content of the memory page is obtained fromthe buffer and is stored to the snapshot file of the virtual machine ifthe memory page is a contaminated memory page. In this way, consistencyof the device status and the virtual memory in the snapshot file, whichis at the first moment, of the virtual machine can be ensured. Inaddition, after the content of the memory page is stored to the buffer,the interception on the contamination action is released such that thememory page can be successfully accessed without a need of suspendingthe virtual machine until the snapshot file of the virtual memory of thevirtual machine has been obtained and then restoring the virtual machineto the running state. This avoids a case in which the service isaffected because the virtual machine is suspended for a relatively longtime.

According to any possible implementation of the first aspect, in aneighth possible implementation, a method for intercepting thecontamination action on the memory page includes enabling writeprotection on the memory page using a userfaultfd interface to interceptthe contamination action on the memory page using the write protection.Further, the memory page is flagged as read-only using the userfaultfdinterface. A copy-on-write function of a LINUX userfault function on amemory page does not distinguish whether a write-protected page ismodified in the kernel mode or in the user mode. In this embodiment ofthe present disclosure, the write protection on the memory page isenabled by invoking the userfaultfd interface without distinguishingwhether the write-protected page is modified in the kernel mode or inthe user mode. Therefore, kernel-driven code does not need to bemodified, and compatibility with an existing virtualization platform canbe implemented.

According to any possible implementation of the first aspect, in a ninthpossible implementation, a method for storing the content of the memorypage to the buffer includes creating a write protection exceptionhandling thread, obtaining, by the write protection exception handlingthread, a file descriptor, where optionally, the write protectionexception handling thread can obtain the file descriptor using theuserfaultfd interface, obtaining, by the write protection exceptionhandling thread, an address of the memory page according to the filedescriptor when the contamination action on the memory page isintercepted, and obtaining, by the write protection exception handlingthread, the content of the memory page according to the address of thememory page, and storing the content to the buffer, where optionally,the write protection exception handling thread can obtain the address ofthe memory page by invoking a function read (ufd), the ufd is a filedescriptor.

According to the ninth possible implementation of the first aspect, in atenth possible implementation, the write protection exception handlingthread may further flag the memory page as “contamination actionintercepted” or “stored to the buffer”. Preferably, the write protectionexception handling thread can flag the memory page using a bitmap. Afterthe write protection exception handling thread stores the content of thememory page to the buffer, the userfaultfd interface is used to instructto remove the write protection on the memory page in the kernel mode,and the virtual machine continues running and can implement a writeoperation on the memory page. When content of memory pages correspondingto all addresses has been stored to the snapshot file, the writeprotection exception handling thread is instructed to exit.

According to any possible implementation of the first aspect, in aneleventh possible implementation, guest software and a virtual machinemonitor on a virtualization platform can contaminate the memory of thevirtual machine. The virtual machine monitor enables an extended pagetable (EPT) or nested page table (NPT) write protection function tointercept a contamination action of the guest software on the memory ofthe virtual machine. After the EPT or NPT write protection function isenabled, the write protection function is used to enable the virtualmachine to switch from a guest state to a host state before the guestsoftware contaminates the memory page, and the virtual machine monitorstores, in the host state, the content of the memory page to the buffer,flags the memory page as “contamination action intercepted” or “storedto the buffer”, and then removes the write protection on the memory pagesuch that the guest software can successfully implement the writeoperation on the memory page.

According to the eleventh possible implementation of the first aspect,in a twelfth possible implementation, the virtual machine monitor candistinguish, according to a code execution path of the virtual machinemonitor, whether the virtual machine monitor is to write the memory ofthe virtual machine. When the virtual machine monitor determines,according to the code execution path, that the memory of the virtualmachine is to be contaminated, the virtual machine monitor determineswhether the snapshot file of the virtual machine is being obtainedcurrently. Further, the virtual machine monitor enables a write actiontrack function when receiving the snapshot command. When the writeaction track function is enabled, the virtual machine monitor maydetermine that the snapshot file of the virtual machine is beingobtained currently. When the snapshot file of the virtual machine isbeing obtained currently, and that content of a memory page to becontaminated has not been stored to the buffer, the virtual machinemonitor stores the content of the memory page to the buffer, and thencontaminates the memory page.

According to a second aspect, an embodiment of the present disclosurefurther provides an apparatus for generating a virtual machine snapshotconfigured to implement the foregoing method.

In a first possible implementation of the second aspect, the apparatusfor generating a virtual machine snapshot includes an obtaining module,a first storage module, an interception module, a second storage module,and a restoration module, where each module implements a specificfunction, and the modules cooperate to implement the method forgenerating a virtual machine snapshot that is provided in the firstaspect and the first to the twelfth possible implementations of thefirst aspect.

In a second possible implementation of the second aspect, the apparatusfor generating a virtual machine snapshot is a virtual machine monitor,where the virtual machine monitor includes a Quick Emulator (QEMU)process in a user mode and a KVM module in a kernel mode, and the QEMUprocess is used to perform the method for generating a virtual machinesnapshot that is provided in the first aspect and the first to thetwelfth possible implementations of the first aspect.

According to a third aspect, an embodiment of the present disclosurefurther provides a computer readable storage medium, where the storagemedium stores an instruction or a software module used to implement themethod for generating a virtual machine snapshot that is described inthe first aspect and the first to the twelfth possible implementationsof the first aspect.

In the embodiments of the present disclosure, from the first moment tothe moment at which the content of the memory page in the memory of thevirtual machine is stored to the snapshot file of the virtual machine,the contamination action that affects the content of the memory page isintercepted, and the content of the memory page is stored to the buffersuch that when the memory page of the virtual machine is being stored,if it is detected that the content of the memory page is stored to thebuffer, the content of the memory page is obtained from the buffer andthe content is stored to the snapshot file of the virtual machine. Inthis way, consistency of the device status and the virtual memory in thesnapshot file, which is at the first moment, of the virtual machine canbe ensured. In addition, after the content of the memory page is storedto the buffer, the interception on the contamination action is releasedsuch that the memory page can be successfully accessed without a need ofsuspending the virtual machine until the snapshot file of the virtualmemory of the virtual machine has been obtained and then restoring thevirtual machine to the running state. This shortens a suspension time ofthe virtual machine, and further mitigates impact on a service carriedby the virtual machine.

BRIEF DESCRIPTION OF DRAWINGS

FIG. 1 is a simplified deployment diagram of a data center according toan embodiment of the present disclosure;

FIG. 2 is a schematic structural block diagram of a computing node inthe data center shown in FIG. 1;

FIG. 3 is a schematic flowchart of an embodiment of a method forgenerating a virtual machine snapshot according to the presentdisclosure;

FIG. 4A and FIG. 4B are a schematic flowchart of another embodiment of amethod for generating a virtual machine snapshot according to thepresent disclosure;

FIG. 5A and FIG. 5B are a schematic flowchart of another embodiment of amethod for generating a virtual machine snapshot according to thepresent disclosure; and

FIG. 6 is a schematic diagram of an embodiment of an apparatus forgenerating a virtual machine snapshot according to the presentdisclosure.

DESCRIPTION OF EMBODIMENTS

To make persons skilled in the art better understand the solutions ofthe present disclosure, the following clearly describes the technicalsolutions in the embodiments of the present disclosure with reference tothe accompanying drawings in the embodiments of the present disclosure.The described embodiments are merely some but not all of the embodimentsof the present disclosure. All other embodiments obtained by persons ofordinary skill in the art based on the embodiments of the presentdisclosure without creative efforts shall fall within the protectionscope of the present disclosure.

In the specification, claims, and accompanying drawings of the presentdisclosure, the terms “first”, “second”, “third”, “fourth”, and so onare intended to distinguish between different objects but do notdescribe a particular order. In addition, the terms “including”,“having”, or any other variant thereof, are intended to cover anon-exclusive inclusion. For example, a process, a method, a system, aproduct, or a device that includes a series of steps or units is notlimited to the listed steps or units, but optionally further includes anunlisted step or unit, or optionally further includes another inherentstep or unit of the process, the method, the product, or the device.

A method for generating a virtual machine snapshot that is provided inan embodiment of the present disclosure may be typically used in abackup and recovery scenario of a data center. FIG. 1 shows a simplifieddeployment diagram of the data center.

Referring to FIG. 1, the data center includes multiple clusters (twoclusters are used as an example in FIG. 1), and each cluster includesmultiple computing nodes (or referred to as hosts), for example, acluster 12 in FIG. 1 includes computing nodes 1-n. Each computing nodeincludes computer software (a virtualization platform) and hardware(including computing hardware, such as x86, an Advanced reducedinstruction set computing (RISC) Machine (ARM), storage hardware, andnetwork hardware), and multiple virtual machines are allowed to run oneach computing node. In addition, the data center further has amanagement server 10 (which may be considered as a management node), anda backup server 11 (there may be multiple backup servers), and thebackup server 11 usually has a large-capacity storage device. As shownin FIG. 1, the management server 10 delivers a backup command for avirtual machine of a cluster, a computing node takes a snapshot of thespecified virtual machine, and corresponding snapshot data is stored tothe backup server. Using a snapshot file, the virtual machine can berestored or a new virtual machine can be created on an original clusternode when needed. Alternatively, cross-site backup data recovery may beused, that is, virtual machine snapshot data of a site can betransferred to another site, and recovered to any virtual machine. Avirtual machine snapshot means storing a status of a virtual machine ata moment like a photo. Usually, a snapshot stores all hard diskinformation, memory information, and central processing unit (CPU)information, and some virtual machine snapshots further store BasicInput/Output System (BIOS) information. A snapshot is usually dividedinto two main parts, a magnetic disk snapshot and a virtual machinestatus snapshot (also referred to as a virtual machine memory snapshotbecause content of most snapshot files is memory data). The magneticdisk snapshot may be independently used, and the virtual machine statussnapshot generally needs to be used with the magnetic disk snapshot. Themethod for generating a virtual machine snapshot in the presentdisclosure is used to store a working status of one or multiple virtualmachines in a computing node to a specified file without interruptingnormal running of a current virtual machine, and belongs to an onlinesnapshot technology.

The following describes the method for generating a virtual machinesnapshot in the present disclosure using a computing node as an example.The computing node may be a server, or a hardware device having anoperation processing capability. A schematic diagram of an internalstructure of the computing node is shown in FIG. 2.

As shown in FIG. 2, FIG. 2 is a schematic structural block diagram of acomputing node according to an embodiment of the present disclosure. Ahardware platform of the computing node includes a processor, a memory,a communications interface, and the like. The memory may include avolatile memory (such as a random access memory (RAM)) and an optionalnonvolatile memory (such as a magnetic disk or a solid state drive(SSD)). Components on the hardware platform may be connected using a busto implement information exchange between the components. The hardwareplatform may alternatively be distributed on different physicalentities, and when the hardware platform is located in differentphysical entities, the information exchange between the components maybe implemented using a communications interface on each computing node.Optionally, the computing node may communicate with a shared storagedevice using the communications interface.

A virtual machine monitor runs on the hardware platform of the computingnode. The virtual machine monitor is configured to mask a hardwaredifference of a hardware platform to provide a user with multipleabstract, unified, and simulated computing environments, that is,multiple virtual machines. The virtual machine monitor simulates, foreach virtual machine, a virtual hardware environment independent of realphysical hardware. The virtual machine includes virtual hardware andguest software. The virtual hardware includes virtual memory, a virtualprocessor, a virtual network interface card, and another virtual device.The guest software includes a user program and a guest operating system.The guest operating system may be WINDOWS, LINUX, SOLARIS, or the like.This is not limited herein.

On some platforms, the virtual machine monitor is an operating system,for example, in a KVM, the virtual machine monitor is a host operatingsystem.

Further, for example, in the KVM, the virtual machine monitor includes aQEMU process in a user mode and a KVM module in a kernel mode. The QEMUprocess is used to simulate the virtual network interface card andanother virtual device of the virtual machine. The KVM module assiststhe QEMU process, and makes full use of a hardware virtualizationcapability to simulate the memory and the processor of the virtualmachine.

A snapshot of the virtual machine is used to back up and recover thevirtual machine. In FIG. 1, the management server 10 delivers a backupcommand for a virtual machine of a cluster. When receiving a snapshotinstruction, the virtual machine monitor obtains status data of thevirtual machine according to the snapshot instruction, and stores thestatus data to a snapshot file. The snapshot file of the virtual machineincludes device status data of the virtual machine and virtual memorydata of the virtual machine. For ease of description, “virtual memory ofthe virtual machine” is referred to as “memory of the virtual machine”in the following. During deployment of a cluster system, a sharedstorage is usually used to store a magnetic disk file and a snapshotfile of each virtual machine in a server to facilitate centralizedmanagement. Certainly, the snapshot file of the virtual machine mayalternatively be stored to a nonvolatile memory of the server. This isnot limited herein.

When the snapshot file of the virtual machine is being obtained,consistency of a device status snapshot and a virtual memory snapshot ofthe virtual machine needs to be ensured, that is, the device status dataand the memory data of the virtual machine need to be the same as devicestatus data and memory data, which are at a specific moment, of thevirtual machine respectively in order to back up and/or restore thevirtual machine according to the snapshot file of the virtual machinesubsequently.

For example, according to a snapshot file, which is at a first moment,of the virtual machine, the virtual machine can be restored to a statusat the first moment, or a new virtual machine is additionally created ona node in the cluster or on a node in another cluster, and the newvirtual machine is enabled to have the status, which is at the firstmoment, of the virtual machine according to the snapshot file, which isat the first moment, of the virtual machine.

FIG. 3 is a schematic flowchart of an embodiment of a method forgenerating a virtual machine snapshot according to the presentdisclosure. As shown in FIG. 3, the method for generating a virtualmachine snapshot includes the following steps.

Step 301. A virtual machine monitor suspends a virtual machine at afirst moment according to a received snapshot command, and starts toperform step 304 and step 305, where the snapshot command is used toinstruct to obtain a snapshot file, which is at the first moment, of thevirtual machine.

In an embodiment, the snapshot command carries information used toindicate the virtual machine, and is used to instruct to obtain thesnapshot file of the virtual machine. After receiving the snapshotcommand, the virtual machine monitor immediately responds to thesnapshot command, and obtains a snapshot file, which is at a moment atwhich the snapshot command is received, of the virtual machine. For easeof description, a moment at which the virtual machine monitor respondsto the snapshot command after receiving the snapshot command is referredto as the first moment. It should be noted that the moment at which thevirtual machine monitor responds to the snapshot command is the same asthe moment at which the snapshot command is received, but there may be adelay from the moment at which the virtual machine monitor receives thesnapshot command to the moment at which the virtual machine monitorresponds to the snapshot command.

After receiving the snapshot command, the virtual machine monitorsuspends the virtual machine at the first moment according to thesnapshot command, and starts to perform a storage operation on a memorypage in memory of the virtual machine and a contamination interceptionoperation on the memory page in the memory of the virtual machine fromthe first moment. The contamination interception operation on the memorypage in the memory of the virtual machine is explained in step 304, andthe storage operation on the memory page in the memory of the virtualmachine is explained in step 305.

Step 302. The virtual machine monitor stores a device status, which isat the first moment, of the virtual machine in a suspended state to thesnapshot file of the virtual machine.

Because the virtual machine is suspended at the first moment, thesnapshot file of the virtual machine is obtained by the virtual machinemonitor includes the suspended device state, which is at the firstmoment, of the virtual machine. How to obtain the snapshot file of thedevice status belongs to other approaches, and details are not describedherein.

Step 303. The virtual machine monitor restores the virtual machine fromthe suspended state to a running state after the device status isstored.

There are a relatively large quantity of memory pages in the memory ofthe virtual machine, and it takes a relatively long time for the virtualmachine monitor to obtain snapshot files of all the memory pages in thememory of the virtual machine. Therefore, if the virtual machine isrestored to the running state after all the snapshot files of the memoryof the virtual machine are obtained, the virtual machine may besuspended for a relatively long time, and impact on a service of thevirtual machine is relatively large. Therefore, in this embodiment, thevirtual machine monitor restores the virtual machine to the runningstate after obtaining the snapshot file of the device status, which isat the first moment, of the virtual machine such that the virtualmachine is suspended for a relatively short time, and the impact on theservice of the virtual machine is relatively small.

Step 304. A contamination interception operation performed by thevirtual machine monitor on a memory page in memory of the virtualmachine includes intercepting, from the first moment to a moment atwhich content of the memory page is stored to the snapshot file of thevirtual machine, a contamination action that affects the content of thememory page, and after storing the content of the memory page to abuffer, releasing the interception on the contamination action.

Step 305. A storage operation performed by the virtual machine monitoron the memory page in the memory of the virtual machine includes storingthe content, stored in the buffer, of the memory page to the snapshotfile of the virtual machine.

In this specification, the contamination action on the memory page is awrite action on the memory page. From the first moment, memory contentof the virtual machine is consistent with memory content at the firstmoment, provided that memory pages in the memory of the virtual machineare not contaminated. However, because it takes a relatively long timeto complete a memory snapshot of the virtual machine, some memory pagesin the memory of the virtual machine may be contaminated.

Therefore, in the embodiment shown in FIG. 3, snapshot files of thememory pages are sequentially obtained from the first moment, and forany memory page in the memory of the virtual machine, if a contaminationaction that affects content of the memory page occurs before the contentof the memory page is stored to the snapshot file of the virtualmachine, the contamination action that affects the content of the memorypage is intercepted. When the contamination action on the memory page isintercepted, the content of the memory page is stored to the buffer, andthen the interception on the contamination action is released. An entityfor performing the contamination action initiates the contaminationaction again, and successfully performs a write operation on the memorypage. In this way, the content, stored in the buffer, of the memory pageis content, which is at the first moment, of the memory page. After amoment at which the content of the memory page is stored to the snapshotfile of the virtual machine, the interception operation on thecontamination action on the memory page no longer needs to be performed.

In the embodiment shown in FIG. 3, the storage operation on the memorypage in the memory of the virtual machine and the contaminationinterception operation on the memory page in the memory are two parallelactions after the first moment. During a process of sequentiallyobtaining the snapshot files of the memory pages in the memory of thevirtual machine, before a snapshot file of one memory page of the memorypages is obtained, whether content of the memory page has been stored tothe buffer is determined first. If the content of the memory page hasnot been stored to the buffer, it indicates that the memory page has notbeen contaminated from the first moment to a current moment, andtherefore, the content of the memory page is directly stored to thesnapshot file. If the content of the memory page has been stored to thebuffer, it indicates that the memory page has been contaminated from thefirst moment to a current moment, and then the content of the memorypage in the memory is not stored to the snapshot file of the virtualmachine, but instead, the buffer is searched for the content of thememory page, and the content, in the buffer, of the memory page isstored to the snapshot file of the virtual machine.

The buffer may be memory other than the memory of the virtual machine,or may be multiple files other than the memory of the virtual machine.This is not limited herein.

There are multiple methods for determining whether the content of thememory page has been stored to the buffer. For example, from the firstmoment to a moment at which the snapshot file of the memory page isstored, the memory page is flagged as “contamination action intercepted”when the contamination action that affects the content of the memorypage is intercepted, or the memory page is flagged as “stored to thebuffer” when the content of the memory page is stored to the buffer.

In this way, before the snapshot files of the memory pages of thevirtual machine are sequentially obtained, when it is detected that thememory page is flagged as “contamination action intercepted” or “storedto the buffer”, it indicates that the memory page has been contaminated,that is, current content of the memory page is inconsistent with thecontent at the first moment, and then the current content of the memorypage is not stored to the snapshot file of the virtual machine, butinstead, the content of the memory page (that is, the content at thefirst moment) is obtained from the buffer, and the obtained content isstored to the snapshot file of the virtual machine.

That the memory page is flagged as “contamination action intercepted” or“stored to the buffer” is as follows, where an address of the memorypage is flagged as “contamination action intercepted” or “stored to thebuffer”. When content of a memory page of the virtual machine is beingstored to the snapshot file of the virtual machine, an address of thememory page is obtained, it is detected whether the address of thememory page is flagged, and if the address of the memory page is notflagged, the content of the memory page is obtained according to theaddress of the memory page and is stored to the snapshot file of thevirtual machine.

In the embodiment shown in FIG. 3, for any memory page in the memory ofthe virtual machine, from the first moment to a moment at which contentof the memory page is stored to the snapshot file of the virtualmachine, a contamination action that affects the content of the memorypage is intercepted, and the content of the memory page is stored to thebuffer such that when the memory page of the virtual machine is beingstored, if it is detected that the content of the memory page has beenstored to the buffer, the content of the memory page is obtained fromthe buffer, and the content is stored to the snapshot file of thevirtual machine. In this way, consistency of the device status and thevirtual memory in the snapshot file, which is at the first moment, ofthe virtual machine can be ensured. In addition, after the content ofthe memory page is stored to the buffer, the interception on thecontamination action is released such that the memory page can besuccessfully accessed without a need of suspending the virtual machineuntil the snapshot file of the virtual memory of the virtual machine hasbeen obtained and then restoring the virtual machine to the runningstate. This avoids a case in which the service is affected because thevirtual machine is suspended for a relatively long time. In addition, inthis embodiment, the buffer is used to store the content of the memorypage of the virtual machine, and therefore, a user may set a size of thebuffer according to an actual requirement.

In the embodiment shown in FIG. 3, there are multiple methods forintercepting the contamination action on the memory page, and thefollowing uses two of the methods as examples for description.

Example 1: Write protection on the memory page is enabled using auserfaultfd interface to intercept the contamination action on thememory page using the write protection.

Further, the memory page is flagged as read-only using the userfaultfdinterface. In this embodiment, after the contamination action on thememory page is intercepted, the content of the memory page needs to bestored to the buffer. The following describes, using an example, amethod for storing the content of the memory page to the buffer.

The method includes creating a write protection exception handlingthread, obtaining, by the write protection exception handling thread, afile descriptor, where the write protection exception handling threadcan obtain the file descriptor using the userfaultfd interface,obtaining, by the write protection exception handling thread, an addressof the memory page according to the file descriptor when thecontamination action on the memory page is intercepted, obtaining, bythe write protection exception handling thread, the content of thememory page according to the address of the memory page, and storing thecontent to the buffer.

The file descriptor is used for communication between a kernel mode anda user mode. After the write protection exception handling thread iscreated, because the memory pages are in a write protection state, whena memory page is to be contaminated, a page fault occurs in the kernelmode, the contamination action is blocked in the kernel mode, and at thesame time, the write protection exception handling thread in the usermode is notified of an address of the memory page using the filedescriptor. The write protection exception handling thread obtainscontent of the memory page according to the address of the memory page,and stores the content of the memory page to the buffer.

In addition, the write protection exception handling thread may furtherflag the memory page as “contamination action intercepted” or “stored tothe buffer”. After the write protection exception handling thread storesthe content of the memory page to the buffer, the userfaultfd interfaceis used to instruct to remove the write protection on the memory page inthe kernel mode. When attempting to contaminate the memory page again,the virtual machine monitor successfully implements the contamination,and continues running.

In Example 1, the write protection on the memory page is enabled byinvoking the userfaultfd interface, whether a write-protected page ismodified in the kernel mode or in the user mode does not need to bedistinguished. Therefore, kernel-driven code does not need to bemodified, and compatibility with an existing virtualization platform canbe implemented.

For ease of understanding, the following describes the method in Example1 using a specific embodiment.

As shown in FIG. 4A and FIG. 4B, FIG. 4A and FIG. 4B are a schematicflowchart of an embodiment of a method for generating a virtual machinesnapshot according to the present disclosure.

Step S41. A control thread in a virtual machine monitor receives asnapshot command.

In this embodiment, the snapshot command is used to instruct to obtain asnapshot, which is at a first moment, of a virtual machine. Further, thecontrol thread may be a QEMU thread in the virtual machine monitor.

Step S42. The control thread creates a write protection exceptionhandling thread.

Step S43. The write protection exception handling thread obtains a filedescriptor ufd by invoking a userfaultfd interface.

Step S44. The control thread suspends the virtual machine.

Further, the control thread suspends the virtual machine at the firstmoment. It should be noted that both step S44 and step S42 are performedafter step S41, and have no sequential order.

Step S45. The control thread stores a device status of the virtualmachine to a snapshot file of the virtual machine.

Because the virtual machine is suspended at the first moment, theobtained device status of the virtual machine is the same as a devicestatus at the first moment.

Step S46. The control thread sets write protection on memory of thevirtual machine by invoking the userfaultfd interface.

It should be noted that step S45 and step S46 have no sequential order.

Step S47. The control thread creates a snapshot thread.

It should be noted that step S47 is performed after step S44 and stepS46, and step S47 and step S45 have no certain sequential order.

Step S48. The control thread restores the virtual machine to a runningstate.

In the embodiment shown in FIG. 4A and FIG. 4B, the following steps areperformed by the write protection exception handling thread in step S42.

Step S420. The write protection exception handling thread waits for awrite protection exception handling request.

After the control thread enables the write protection on the memory ofthe virtual machine using the userfaultfd interface, when a memory pageis to be contaminated, a page fault occurs in a kernel mode, thecontamination action is blocked, and then the write protection exceptionhandling thread is notified of an address of the memory page using thefile descriptor. In this embodiment, the write protection exceptionhandling request means that the write protection exception handlingthread is notified, in the kernel mode, of the address of the memorypage to be contaminated.

Step S421. The write protection exception handling thread obtains anaddress of a memory page with a write protection exception by invokingread (ufd).

When the write protection exception handling thread is notified, in thekernel mode, of the address of the memory page to be contaminated, thewrite protection exception handling thread obtains the address of thememory page by invoking the read (ufd).

Step S422. The write protection exception handling thread stores contentof the memory page to a buffer according to the address of the memorypage.

Step S423. The write protection exception handling thread flags thememory page as “contamination action intercepted” or “stored to thebuffer”.

There are multiple methods for flagging the memory page. For example, abitmap may be used to flag the memory page. For example, a variableuserfault_bitmap may be defined, and each bit of the variablecorresponds to an address of a memory page of the virtual machine.Further, according to any bit in the variable, an address of a memorypage corresponding to the bit may be calculated. An initial value of acorresponding bit, of each memory page, in the userfault_bitmap is 0.After the write protection exception handling thread stores the contentof the memory page to the buffer, the memory page is flagged by changinga corresponding bit, of the memory page, in the userfault_bitmap to 1.Certainly, the content of the memory page may also be stored to thebuffer after the memory page is flagged. This is not limited herein.

Step S424. The write protection exception handling thread removes writeprotection on the memory page by invoking the userfaultfd interface suchthat execution of an action of contaminating the memory page resumes.

Step S425. The write protection exception handling thread ends whenreceiving an exit notification.

Further, the write protection exception handling thread ends and exitswhen receiving the exit notification from the snapshot thread.

In the embodiment shown in FIG. 4A and FIG. 4B, the snapshot thread instep S47 performs the following steps.

Step S471. Sequentially obtain an address of a memory page.

Step S472. Determine whether content of the memory page corresponding tothe address has been stored to the buffer, and perform steps S473 andS474 if the content of the memory page corresponding to the address hasnot been stored to the buffer, or perform step S475 if the content ofthe memory page corresponding to the address has been stored to thebuffer.

Further, a corresponding bit, of the memory page, in theuserfault_bitmap is searched for. If the corresponding bit, of thememory page, in the userfault_bitmap is 0, it is determined that thememory page has not been flagged, that is, the content of the memorypage has not been stored to the buffer. If the corresponding bit is 1,it is determined that the memory page has been flagged, that is, thecontent of the memory page has been stored to the buffer.

Step S473. Directly store the content of the memory page to the snapshotfile of the virtual machine.

Step S474. Remove the write protection on the memory page by invokingthe userfaultfd interface.

Step S475. Read the content of the memory page from the buffer, andstore the content to the snapshot file of the virtual machine.

Step S476. Determine whether content of memory pages corresponding toall addresses has been stored to the snapshot file, and if the contentof the memory pages corresponding to all the addresses has been storedto the snapshot file, instruct the write protection exception handlingthread to exit, and end the snapshot thread, or if the content of thememory pages corresponding to all the addresses has not been stored tothe snapshot file, repeat steps S471 to S476.

There are multiple methods for determining whether the content of thememory pages corresponding to all the addresses has been stored to thesnapshot file. For example, a bitmap may be used to flag a memory page.For example, a variable snapshot_bitmap may be defined, and each bit ofthe variable corresponds to an address of a memory page of the virtualmachine. Further, according to any bit in the variable, an address of amemory page corresponding to the bit may be calculated. If acorresponding bit, of a memory page, in the snapshot_bitmap is 1, itindicates that content of the memory page has not been stored to thesnapshot file of the virtual machine. If a corresponding bit, of amemory page, in the snapshot_bitmap is 0, it indicates that content ofthe memory page has been stored to the snapshot file of the virtualmachine. Each time the snapshot thread stores content of a memory pageto the snapshot file of the virtual machine, a corresponding bit, of thememory page, in the snapshot_bitmap is changed to 0.

When it is detected that all corresponding bits, of all the memory pagesof the virtual machine, in the snapshot_bitmap are 0, it may bedetermined that the content of the memory pages corresponding to all theaddresses has been stored to the snapshot file. If it is detected thatnot all corresponding bits, of all the memory pages, in thesnapshot_bitmap are 0, the foregoing steps are repeated.

Example 2: Guest software and a virtual machine monitor on avirtualization platform can contaminate the memory of the virtualmachine. The virtual machine monitor can distinguish, using a codeexecution path of the virtual machine monitor, whether the virtualmachine monitor is to write the memory of the virtual machine. However,during normal running, the virtual machine monitor cannot sense that theguest software is to contaminate the memory of the virtual machine.

Therefore, in Example 2, the virtual machine monitor enables an EPT orNPT write protection function to intercept a contamination action of theguest software on the memory of the virtual machine. Further, after theEPT or NPT write protection function is enabled, the write protectionfunction is used to enable the virtual machine to switch from a gueststate to a host state before the guest software contaminates a memorypage, and the virtual machine monitor stores, in the host state, contentof the memory page to the buffer, flags the memory page as“contamination action intercepted” or “stored to the buffer”, and thenremoves write protection on the memory page such that the guest softwarecan successfully implement the contamination action on the memory page.

For the virtual machine monitor, the virtual machine monitor candetermine, using the code execution path, whether the memory of thevirtual machine is to be contaminated. Therefore, when the virtualmachine monitor determines, according to the code execution path, thatthe memory of the virtual machine is to be contaminated, the virtualmachine monitor determines whether the snapshot file of the virtualmachine is being obtained currently. Further, the virtual machinemonitor enables a write action track function when receiving thesnapshot command. When determining that the write action track functionis enabled, the virtual machine monitor may determine that the snapshotfile of the virtual machine is being obtained currently.

When the snapshot file of the virtual machine is being obtainedcurrently, and that content of a memory page to be contaminated has notbeen stored to the buffer, the virtual machine monitor stores thecontent of the memory page to the buffer, and then contaminates thememory page.

In a process of obtaining the snapshot file of the virtual machine, thevirtual machine monitor flags the memory page as “stored to the buffer”when storing the content of the memory page to the buffer. Further, thememory page may be flagged by recording an address of the memory page.Certainly, the above is merely an example, and constitutes nolimitation.

For ease of understanding, the following describes the method in Example2 using a specific embodiment.

As shown in FIG. 5A and FIG. 5B, FIG. 5A and FIG. 5B are a schematicflowchart of an embodiment of a method for generating a virtual machinesnapshot according to the present disclosure.

Step S51. A control thread receives a snapshot command.

In this embodiment, the snapshot command is used to instruct to obtain asnapshot, which is at a first moment, of a virtual machine. Further, thecontrol thread may be a QEMU thread.

Step S52. The control thread creates a snapshot thread.

Step S53. The control thread suspends a virtual machine.

Further, the control thread suspends the virtual machine when receivingthe snapshot command.

Step S54. The control thread stores a device status of the virtualmachine to a snapshot file of the virtual machine.

Because the virtual machine is suspended at the first moment, theobtained device status of the virtual machine is the same as a devicestatus at the first moment.

Step S55. The control thread enables an EPT or NPT write protectionfunction and a write action track function.

The control thread enables the EPT/NPT write protection function ofmemory of the virtual machine when receiving a notification indicatingthat there is a snapshot request. The function enables guest software toexit when the guest software attempts to contaminate a memory page ofthe virtual machine such that the memory page cannot be contaminated.When the guest software exits, the virtual machine monitor can detectthat the guest software is to perform an action of contaminating thememory of the virtual machine.

In addition, the virtual machine monitor may also distinguish, accordingto a code execution path, whether the memory of the virtual machine isto be written to detect whether the virtual machine monitor is toperform an action of contaminating the memory of the virtual machine.The enabled write action track function can enable the virtual machinemonitor to determine that the virtual machine monitor is currently at astage of obtaining the snapshot file of the virtual machine such thatthe virtual machine monitor first determines, after detecting that thevirtual machine monitor is to perform the action of contaminating thememory of the virtual machine, whether content of a memory page to becontaminated has been stored to the buffer, and then stores the contentof the memory page to the buffer if the content of the memory page to becontaminated has not been stored to the buffer.

Step S56. The control thread restores the virtual machine to a runningstate.

In the virtual machine monitor, after it is detected that the action ofcontaminating the memory of the virtual machine occurs, the virtualmachine monitor performs the following steps.

Step S61. Determine whether a snapshot of memory of the virtual machineneeds to be obtained, and perform step S52 to step S66 if the snapshotof the memory of the virtual machine needs to be obtained, or continueto perform an action of contaminating the memory of the virtual machineif the snapshot of the memory of the virtual machine does not need to beobtained.

Further, when it is detected that the EPT or NPT write protectionfunction and the write action track function are enabled, it may bedetermined that the snapshot file of the virtual machine is beingobtained currently. Therefore, step S62 to step S66 are performed. If itis detected that the EPT or NPT write protection function and the writeaction track function are not enabled, it indicates that the virtualmachine monitor is not at the stage of obtaining the snapshot file ofthe virtual machine, and therefore the action of contaminating thememory of the virtual machine continues to be performed normally.

S62. Determine whether content of a memory page to be contaminated hasbeen stored to a buffer, and perform step S63 if the content of thememory page to be contaminated has not been stored to the buffer, orcontinue to perform the contamination action if the content of thememory page to be contaminated has been stored to the buffer.

In a process of obtaining the snapshot file of the virtual machine, asame memory page may be contaminated multiple times. Therefore, thevirtual machine monitor may flag the memory page when storing content ofthe memory page to the buffer. In this way, when detecting that theaction of contaminating the memory of the virtual machine occurs, thevirtual machine monitor determines, before storing the content of thememory page to the buffer, whether the content of the memory page hasbeen stored to the buffer by determining whether the memory page isflagged. If the content of the memory page has been stored to thebuffer, the content of the memory page no longer needs to be stored tothe buffer, and the contamination action continues to be performed.

Further, a dirty bitmap (dirty_bitmap) is set in the virtual machinemonitor, and an address of each memory page of the virtual machine has acorresponding bit in the dirty_bitmap. When an initial value of acorresponding bit, of an address of a memory page, in the dirty bitmapis 0, it indicates that content of the memory page has not been storedto the buffer. The virtual machine monitor changes the correspondingbit, of the address of the memory page, in the dirty bitmap to 1 whenstoring the content of the memory page to the buffer. In this way, whenthe virtual machine monitor detects that the corresponding bit, of theaddress of the memory page, in the dirty bitmap is 1, it indicates thatthe content of the memory page has been stored to the buffer.

Step S63. Flag the memory page.

Step S64. Determine whether an entity for performing the contaminationaction is the virtual machine monitor, and perform step S65 if theentity for performing the contamination action is the virtual machinemonitor, or perform step S65 and step S66 if the entity for performingthe contamination action is not the virtual machine monitor.

When the virtual machine monitor detects the action of contaminating thememory of the virtual machine, the entity for performing thecontamination action may be the virtual machine monitor itself, or maybe the guest software. If the virtual machine determines that the entityfor performing the contamination action is not the virtual machinemonitor, it may be determined that the entity is the guest software.

Step S65. Store the content of the memory page to the buffer.

Step S66. Remove write protection on the memory page, and resume thecontamination action of guest software.

In the embodiment shown in FIG. 5A and FIG. 5B, the snapshot thread instep S52 performs the following steps.

Step S521. Sequentially obtain an address of a memory page.

Step S522. Determine whether content of the memory page corresponding tothe address has been stored to the buffer, and perform step S523 if thecontent of the memory page corresponding to the address has not beenstored to the buffer, or perform step S524 if the content of the memorypage corresponding to the address has been stored to the buffer.

Further, a corresponding bit, of the memory page, in the dirty bitmapdirty_bitmap is searched for. If the corresponding bit, of the memorypage, in the dirty_bitmap is 0, it is determined that the memory pagehas not been flagged, that is, the content of the memory page has notbeen stored to the buffer. If the corresponding bit is 1, it isdetermined that the memory page has been flagged, that is, the contentof the memory page has been stored to the buffer.

Step S523. Directly store the content of the memory page to the snapshotfile of the virtual machine.

Step S524. Read the content of the memory page from the buffer, andstore the content to the snapshot file of the virtual machine.

Step S525. Determine whether content of memory pages corresponding toall addresses has been stored to the snapshot file, and perform stepS526 and step S527 if the content of the memory pages corresponding toall the addresses has been stored to the snapshot file, or perform stepS521 to step S525 if the content of the memory pages corresponding toall the addresses has not been stored to the snapshot file.

Step S526. Disable the memory write protection function and the writeaction track function.

Step S527. End the snapshot thread.

The foregoing describes the method for generating a virtual machinesnapshot in the present disclosure, and the following describes anapparatus for generating a virtual machine snapshot in the presentdisclosure. The apparatus for generating a virtual machine snapshot isconfigured to perform the foregoing method for generating a virtualmachine snapshot.

Referring to FIG. 6, FIG. 6 is a schematic structural diagram of anembodiment of an apparatus for generating a virtual machine snapshotaccording to the present disclosure. As shown in FIG. 6, the apparatusfor generating a virtual machine snapshot includes an obtaining module601 configured to suspend a virtual machine at a first moment accordingto a received snapshot command, where the snapshot command is used toinstruct to obtain a snapshot file, which is at the first moment, of thevirtual machine, a first storage module 602 configured to start toperform a storage operation on a memory page in memory of the virtualmachine from the first moment, an interception module 603 configured tostart to perform a contamination interception operation on the memorypage in the memory from the first moment, a second storage module 604configured to store a device status, which is at the first moment, ofthe virtual machine in a suspended state to the snapshot file of thevirtual machine, and a restoration module 605 configured to restore thevirtual machine from the suspended state to a running state after thedevice status is stored.

The interception module 603 is further configured to intercept, from thefirst moment to a moment at which content of the memory page is storedto the snapshot file of the virtual machine, a contamination action thataffects the content of the memory page, and after the content of thememory page is stored to a buffer, release the interception on thecontamination action.

The first storage module 602 is further configured to store the content,stored in the buffer, of the memory page to the snapshot file of thevirtual machine.

Optionally, before intercepting the contamination action that affectsthe content of the memory page, the interception module 603 furtherenables write protection on the memory page using a userfaultfdinterface.

Optionally, the interception module 603 is further configured to performthe following operations when storing the content of the memory page tothe buffer creating a write protection exception handling thread,obtaining, by the write protection exception handling thread, a filedescriptor using the userfaultfd interface, obtaining, by the writeprotection exception handling thread, an address of the memory pageaccording to the file descriptor when the contamination action on thememory page is intercepted, obtaining, by the write protection exceptionhandling thread, the content of the memory page according to the addressof the memory page, and storing the content to the buffer.

Optionally, the virtual machine includes guest software. Theinterception module 603 is further configured to enable an EPT or NPTwrite protection function, where the write protection function is usedto enable the virtual machine to switch from a guest state to a hoststate before the guest software contaminates the memory page, and theinterception module 603 is configured to store, in the host state, thecontent of the memory page to the buffer.

Optionally, the interception module 603 is further configured to performthe following operations of determining, according to a code executionpath of the virtual machine monitor, whether the memory of the virtualmachine is to be contaminated, when the memory of the virtual machine isto be contaminated, determining, by the virtual machine monitor, whetherthe snapshot file of the virtual machine is being obtained currently,when the virtual machine monitor determines that the snapshot file ofthe virtual machine is being obtained currently, determining, by thevirtual machine monitor, whether content of a memory page to becontaminated has been stored to the buffer, and storing the content ofthe memory page to the buffer when the virtual machine monitordetermines that the content of the memory page to be contaminated hasnot been stored to the buffer.

For more detailed descriptions of the apparatus for generating a virtualmachine snapshot, refer to the foregoing description of the method forgenerating a virtual machine snapshot.

The foregoing describes the apparatus for generating a virtual machinesnapshot in the present disclosure, and the following describes avirtual machine monitor in the present disclosure.

As shown in FIG. 2, a virtual machine monitor runs on a hardwareplatform of a computing node in FIG. 2. The virtual machine monitor isconfigured to mask a hardware difference of a computing platform toprovide a user with multiple abstract, unified, and simulated computingenvironments, that is, multiple virtual machines. The virtual machinemonitor simulates, for each virtual machine, a virtual hardwareenvironment independent of real physical hardware. The virtual machinemonitor is further configured to generate, in an online manner using themethods according to the embodiments shown in FIG. 3 to FIG. 5A and FIG.5B, a snapshot file of a virtual machine running on the virtual machinemonitor.

It may be clearly understood by persons skilled in the art that, for thepurpose of convenient and brief description, for a specific workingprocess of the foregoing system, apparatus, and modules, reference maybe made to a corresponding process in the foregoing method embodiments,and details are not repeated herein.

In the embodiments provided in this application, it should be understoodthat the disclosed system, apparatus, and method may be implemented inother manners. For example, the described apparatus embodiment is merelyan example. For example, the unit division is merely logical functiondivision and may be other division in an actual implementation. Forexample, a plurality of units or components may be combined orintegrated into another system, or some features may be ignored or notperformed. In addition, the shown or discussed mutual couplings ordirect couplings or communication connections may be implemented usingsome interfaces. The indirect couplings or communication connectionsbetween the apparatuses or units may be implemented in electrical,mechanical, or other forms.

The units described as separate parts may or may not be physicallyseparated, and parts shown as units may or may not be physical units,may be located in one position, or may be distributed on a plurality ofnetwork units. Some or all of the units may be selected according toactual requirements to achieve the objectives of the solutions of theembodiments.

In addition, functional units in the embodiments of the presentdisclosure may be integrated into one processing unit, or each of theunits may exist alone physically, or two or more units are integratedinto one unit. The integrated unit may be implemented in a form ofhardware, or may be implemented in a form of a software functional unit.

When the integrated unit is implemented in the form of a softwarefunctional unit and sold or used as an independent product, theintegrated unit may be stored in a computer readable storage medium.Based on such an understanding, the technical solutions of the presentdisclosure essentially, or the part contributing to the otherapproaches, or all or some of the technical solutions may be implementedin the form of a software product. The computer software product isstored in a storage medium and includes instructions for instructing acomputer device (which may be a personal computer, a server, a networkdevice, or the like) to perform all or some of the steps of the methodsdescribed in the embodiments of the present disclosure. The foregoingstorage medium includes any medium that can store program code, such asa universal serial bus (USB) flash drive, a removable hard disk, aread-only memory (ROM), a RAM, a magnetic disk, or an optical disc.

The foregoing embodiments are merely intended for describing thetechnical solutions of the present disclosure, but not for limiting thepresent disclosure. Although the present disclosure is described indetail with reference to the foregoing embodiments, persons of ordinaryskill in the art should understand that they may still makemodifications to the technical solutions described in the foregoingembodiments or make equivalent replacements to some technical featuresthereof, without departing from the scope of the technical solutions ofthe embodiments of the present disclosure.

What is claimed is:
 1. A method for generating a virtual machinesnapshot, comprising: suspending a virtual machine according to areceived snapshot command; enabling write protection on a memory page ofthe virtual machine in a user mode, wherein the write protection causesthe memory page to be blocked in a kernel mode and a write protectionexception to be processed in the user mode; storing the memory page to asnapshot file before the memory page is contaminated; and restoring thevirtual machine after storing the memory page to the snapshot file. 2.The method of claim 1, wherein the write protection is enabled on thememory page using a userfaultfd interface.
 3. The method of claim 1,wherein storing the memory page to the snapshot file comprises: storingcontent of the memory page to a buffer; releasing the write protectionon the memory page after storing the content of the memory page to thebuffer; and storing the content of the memory page from the buffer tothe snapshot file.
 4. The method of claim 3, wherein the content of thememory page is stored to the buffer when the virtual machine is in ahost state.
 5. The method of claim 1, wherein, in response to receivingthe received snapshot command, the method further comprises:intercepting a contamination action affecting content of the memorypage; and starting to store the memory page to the snapshot file afterintercepting the contamination action affecting the content of thememory page.
 6. The method of claim 1, further comprising storing adevice status of the virtual machine in a suspended state to thesnapshot file of the virtual machine after suspending the virtualmachine.
 7. The method of claim 6, further comprising restoring thedevice status of the virtual machine from the suspended state to arunning state after restoring the virtual machine.
 8. A non-transitorycomputer-readable storage medium storing instructions that when executedby a computing device, cause the computing device to be configured to:suspend a virtual machine according to a received snapshot command;enable write protection on a memory page of the virtual machine in auser mode, wherein the write protection causes the memory page to beblocked when the computing device is in a kernel mode and a writeprotection exception to be processed when the computing device is in theuser mode; store the memory page to a snapshot file before the memorypage is contaminated; and restore the virtual machine after the memorypage is stored to the snapshot file.
 9. The non-transitorycomputer-readable storage medium of claim 8, wherein the writeprotection is enabled on the memory page using a userfaultfd interface.10. The non-transitory computer-readable storage medium of claim 8,wherein, the instructions further cause the computing device to beconfigured to: store content of the memory page to a buffer; release thewrite protection on the memory page after the content of the memory pageis stored to the buffer; and store the content of the memory page fromthe buffer to the snapshot file.
 11. The non-transitorycomputer-readable storage medium of claim 10, wherein the content of thememory page is stored to the buffer when the virtual machine is in ahost state.
 12. The non-transitory computer-readable storage medium ofclaim 8, wherein the instructions further cause the computing device tobe configured to: intercept a contamination action affecting content ofthe memory page; and start to store the memory page to the snapshot fileafter intercepting the contamination action affecting the content of thememory page.
 13. The non-transitory computer-readable storage medium ofclaim 8, wherein the instructions further cause the computing device tobe configured to store a device status of the virtual machine in asuspended state to the snapshot file of the virtual machine aftersuspending the virtual machine.
 14. The non-transitory computer-readablestorage medium of claim 13, wherein the instructions further cause thecomputing device to be configured to restore the device status of thevirtual machine from the suspended state to a running state afterrestoring the virtual machine.
 15. A computing node, comprising: amemory comprising instructions; and a processor coupled to the memoryand configured to execute the instructions, which cause the processor tobe configured to: suspend a virtual machine according to a receivedsnapshot command; enable write protection on a memory page of thevirtual machine in a user mode, wherein the write protection causes thememory page to be blocked when the computing node is in a kernel modeand a write protection exception to be processed when the computing nodeis in the user mode; store the memory page to a snapshot file before thememory page is contaminated; and restore the virtual machine after thememory page is stored to the snapshot file.
 16. The computing node ofclaim 15, wherein the write protection is enabled on the memory pageusing a userfaultfd interface.
 17. The computing node of claim 15,wherein the instructions further cause the processor to be configuredto: store content of the memory page to a buffer; release the writeprotection on the memory page after the content of the memory page isstored to the buffer; and store the content of the memory page from thebuffer to the snapshot file.
 18. The computing node of claim 17, whereinthe content of the memory page is stored to the buffer when the virtualmachine is in a host state.
 19. The computing node of claim 15, whereinthe instructions further cause the processor to be configured to:intercept a contamination action affecting content of the memory page;and start to store the memory page to the snapshot file afterintercepting the contamination action affecting the content of thememory page.
 20. The computing node of claim 15, wherein theinstructions further cause the processor to be configured to: store adevice status of the virtual machine in a suspended state to thesnapshot file of the virtual machine after suspending the virtualmachine; and restore the device status of the virtual machine from thesuspended state to a running state after restoring the virtual machine.